import {
  // sendFlowProcessApi,
  closeFlowProcessApi,
  getUrgingWorkItemApi,
  getRecallWorkItemApi,
} from '/@/api/flow/flow';
import { useMessage } from '/@/hooks/web/useMessage';
import { useFlowStoreWithout } from '/@/store/modules/flow';

const { createSuccessModal, createWarningModal } = useMessage();

export async function flowApiFn(params, apiFn, emit) {
  const res = await apiFn(params);

  createSuccessModal({
    iconType: 'success',
    title: '提示',
    content: res.workItemID ? '保存成功' : res,
    onOk: async () => {
      emit('update:visible', false);
      emit('success', false);
      emit('change', false, res.workItemID ? 'save' : 'send');
    },
  });
}

// 流程新建和发送
export async function useFlowButton(code, data, callbacks: Fn[]) {
  const { flowDataRef, flowCheckParams, flowProcessDesc, btnItem, modalInit } = data;
  const [openModal, emit, openComFlowModal] = callbacks;

  const { clearQuery } = btnItem;

  const flowStore = useFlowStoreWithout();

  if (clearQuery) {
    flowStore.setFlowParams(null);
  }

  // 点击按钮验证表单
  async function validateModalForm(workItemDto) {
    const [values, afterSumbitFn] = await modalInit.useModalSumbit(true);

    return [
      {
        ...workItemDto.formMap.resultMap,
        ...values,
        item_id: workItemDto.formMap.resultMap && workItemDto.formMap.resultMap.item_id,
      },
      afterSumbitFn,
    ];
  }

  switch (code) {
    case 'Save':
      const data = await validateModalForm(flowDataRef.workItemDto);
      flowDataRef.workItemDto.formMap.resultMap = data[0];
      openModal(true, { record: flowDataRef.workItemDto, isSave: true, afterSumbitFn: data[1] });
      break;
    case 'Send':
      const ret = await validateModalForm(flowDataRef.workItemDto);
      flowDataRef.workItemDto.formMap.resultMap = ret[0];
      // 申请人保存了并且不用审核和选择审核人

      // if (
      //   flowProcessDesc.isManage &&!flowDataRef.workItemDto.choices.length &&
      //   !flowDataRef.workItemDto.receiverLimit.length
      // ) {
      //   const params = { ...flowDataRef };
      //   await flowApiFn(params, sendFlowProcessApi, emit);
      // } else {

      // }
      const isManage = flowProcessDesc.isManage;
      openModal(true, {
        guid: flowDataRef.guid,
        record: flowDataRef.workItemDto,
        isManage,
        afterSumbitFn: ret[1],
      });
      break;
    case 'Urging':
      console.log('催办');
      const urgingRes = await getUrgingWorkItemApi({ guid: flowDataRef.guid, ...flowCheckParams });

      createSuccessModal({
        iconType: 'success',
        title: '提示',
        content: urgingRes,
        onOk: async () => {
          emit('update:visible', false);
          emit('change', false);
        },
      });

      break;
    case 'Recall':
      console.log('撤回');
      const recallRes = await getRecallWorkItemApi({ guid: flowDataRef.guid, ...flowCheckParams });

      if (!recallRes) {
        createSuccessModal({
          iconType: 'success',
          title: '提示',
          content: '撤回成功',
          onOk: async () => {
            emit('update:visible', false);
            emit('success');
            emit('change', false);
          },
        });
      } else {
        createWarningModal({
          title: '提示',
          content: recallRes,
        });
      }

      break;
    case 'Circulation':
    case 'Consultation':
    case 'TurnToDo':
      // 传阅，意见征询，转批
      openComFlowModal(true, {
        ...btnItem,
        record: null,
        isFlowDetail: true,
        guid: flowDataRef.guid,
        isPassArr: [btnItem.record],
      });
      break;
    default:
      console.log('表单数据', flowProcessDesc.isManage, flowProcessDesc.isCheck);
      await closeFlowProcessApi({ ...flowCheckParams, guid: flowDataRef.guid });
      flowStore.setFlowReleaseParams(null);
      emit('update:visible', false);
      flowProcessDesc.isManage && !flowProcessDesc.isCheck && emit('success', false);
      emit('change', false);
      break;
  }
}
